package com.lzl.eduorder.service.impl;

import com.lzl.common_utils.ordervo.CourseWebVoOrder;
import com.lzl.common_utils.ordervo.UcenterMemberOrder;
import com.lzl.eduorder.client.EduClient;
import com.lzl.eduorder.client.UcenterClient;
import com.lzl.eduorder.entity.Order;
import com.lzl.eduorder.mapper.OrderMapper;
import com.lzl.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzl.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    //生成订单的方法
    @Override
    public String createOrders(String courseId, String memberIdByJwtToken) {

        //通过远程调用--根据用户id获取用户信息
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberIdByJwtToken);

        //通过远程调用--根据课程id获取课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

        //创建Order对象，向order对象里面设置需要的数据
        Order order = new Order();
        //设置订单号---订单号不是id---两个属性
        //随即唯一的、现在使用工具类、使用UUID也行
        order.setOrderNo(OrderNoUtil.getOrderNo());

        //设置值--不能使用BeanUtils工具类--因为属性名不一样
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        //用户
        order.setMemberId(memberIdByJwtToken);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        //订单表的设置
        order.setStatus(0);     //订单状态 0：未支付 1：已支付
        order.setPayType(1);    //支付类型 1：微信  2：支付宝

        //保存到数据库中
        baseMapper.insert(order);

        //返回订单号
        return order.getOrderNo();
    }
}
